package cn.ainit.sys.interceptor;

import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import cn.ainit.sys.constant.SystemConstant;
import cn.ainit.sys.menu.MenuManager;
import cn.ainit.sys.menu.OnlineUserListener;
import cn.ainit.sys.menu.SessionCont;

public class AinitInterceptor  extends HandlerInterceptorAdapter {  

	List<String> excludedUrls;
	MenuManager manager;
	@Resource
	public void setManager(MenuManager manager) {
		this.manager = manager;
	}
	public void setExcludedUrls(List<String> excludedUrls) {
		this.excludedUrls = excludedUrls;
	}
	boolean is_do = false;
	// 编码也可考虑在这设,但还是先使用web.xml配置的filter
	@Override  
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {  
		// System.out.println(request.getRequestURL());
		//获取URI,手动排除过滤
		String uri = request.getRequestURI();
		boolean pass = true;
		for(String s:excludedUrls)
		{
			if(uri.endsWith(s)){
				pass = false;
			}
		}
		if(pass)
		{
			return	func_min(request, response, handler);

		}else{
			return true;
		}

	}  
	private ServletContext getServletContext() {
		return null;
	}
	
	
	
	private boolean func_min(HttpServletRequest request, HttpServletResponse response, Object handler)throws Exception{
		HttpSession session =	request.getSession();
		String id =	(String) session.getAttribute(session.getId());
		String uid =(String)  session.getAttribute(SessionCont.uid);
		int ra=0;
		//判断是否异地登陆,若是则删除session
		List<Map<String, Object>> list_ = manager.querySessionInfo(uid);
		String  lastIp="";
		if(list_.size()>0){
			lastIp=(String) list_.get(0).get("LOGIN_IP");
			String uri = request.getRequestURI();
			if(!request.getRemoteHost().equals(lastIp)){
				if(uri.endsWith("getAllMenu")){
					OnlineUserListener.sessionMap.remove(request.getSession().getId());
					session.removeAttribute(SessionCont.uid);

				}else{
					response.reset();
					request.setCharacterEncoding("UTF-8");
					response.setContentType("text/html;charset=UTF-8");
					PrintWriter out = response.getWriter();
					StringBuilder builder = new StringBuilder();  
					builder.append("<script type=\"text/javascript\">");  
					builder.append(" window.top.location.href=\""); 
					builder.append(SystemConstant.PROJECT_ROOT_URL+"\";</script>");  
					out.print(builder.toString());
					out.flush();
					out.close();
					ra=1;
				}

			} 
		}
		boolean flag = true;  
		request.setCharacterEncoding("UTF-8");  
		Map loangMap  = request.getParameterMap();
		Object loginF = request.getParameter("login");
		if ((loginF != null && "yes".equals(loginF.toString()))|| (SystemConstant.PROJECT_ROOT_URL+"menu/login").equals(request.getRequestURI().toString())) {
			//说明是登录模块    继续登录
			flag = true; 
		}else {
			//判断session是否登录   没有的话  弹出登录窗口
			//登陆失败的时候弹出javascript
			if (session.getAttribute(SessionCont.uid) == null||ra==1) {// 没登录就要求登录 
				flag = false;
				response.reset();
				request.setCharacterEncoding("UTF-8");
				response.setContentType("text/html;charset=UTF-8");
				PrintWriter out = response.getWriter();
				StringBuilder builder = new StringBuilder();  
				builder.append("<script type=\"text/javascript\">");  
				builder.append(" window.top.location.href=\""); 
				builder.append(SystemConstant.PROJECT_ROOT_URL+"\";</script>");  
				out.print(builder.toString());
				out.flush();
				out.close();
			}
			return flag;

		}
		return flag;  

	}

}

